#!/usr/bin/php
<?php
error_reporting(E_ERROR | E_PARSE);

// setup command line args (commandline only)
if (isset($argv[1])) { $datestamp = $argv[1]; }
else {  $datestamp = date("Y-m-d"); }
$testtime = strtotime ($datestamp);
$testtime1 = $testtime - ((86400)*1);
$datestamp1 = date("Y-m-d",$testtime1);
$testtime3 = $testtime - ((86400)*3);
$datestamp3 = date("Y-m-d",$testtime3);
$testtime4 = $testtime - ((86400)*4);
$datestamp4 = date("Y-m-d",$testtime4);
$testtime7 = $testtime - ((86400)*7);
$datestamp7 = date("Y-m-d",$testtime7);
$testtime14 = $testtime - ((86400)*14);
$datestamp14 = date("Y-m-d",$testtime14);
$day_of_the_week = date('l', $testtime);
$full_date = date('l, F j, Y', $testtime);
echo "\n  Query for: ".$full_date."\n";

$expected = array (
    "Monday"    => array("1" => "4 - low",   "2" => "9 - high",  "3" => "4 - low",   "4" => "9 - high",  "5" => "21 - high", "6" => "7 - low", "7" => "2 - none"),
    "Tuesday"   => array("1" => "6 - medium","2" => "6 - medium","3" => "7 - medium","4" => "6 - medium","5" => "21 - high", "6" => "7 - low", "7" => "2 - none"),
    "Wednesday" => array("1" => "9 - high",  "2" => "3- low",    "3" => "9- high",   "4" => "4 - low",   "5" => "21 - high", "6" => "7 - low", "7" => "2 - none"),
    "Thursday"  => array("1" => "6 - medium","2" => "1 - none",  "3" => "9 - high",  "4" => "1 - none",  "5" => "21 - high", "6" => "7 - low", "7" => "2 - none"),
    "Friday"    => array("1" => "3 - low",   "2" => "1 - none",  "3" => "6 - medium","4" => "4 - low",   "5" => "21 - high", "6" => "7 - low", "7" => "2 - none"),
    "Saturday"  => array("1" => "1 - none",  "2" => "4 - low",   "3" => "4 - low",   "4" => "7 - medium","5" => "21 - high", "6" => "7 - low", "7" => "2 - none"),
    "Sunday"    => array("1" => "1 - none",  "2" => "6 - medium","3" => "1 - none",  "4" => "9 - high",  "5" => "21 - high", "6" => "7 - low", "7" => "2 - none")
);
// Username, password and schema for the database connection.
$username="aimatch";
$password="RMSAdmin";
$database="canary";

// Connect to the database and run the query
mysql_connect('w19',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

// Retrieve Current Numbers from the database
$query1 = "select sum(served) from bigsum where date<=\"".$datestamp1."\" and date >=\"".$datestamp3."\" and site=3 and area=11 ";
$query2 = "select sum(served) from bigsum where date<=\"".$datestamp1."\" and date >=\"".$datestamp3."\" and site=4 and area=12 ";
$query3 = "select sum(served) from bigsum where date<=\"".$datestamp1."\" and date >=\"".$datestamp4."\" and site=5 and area=13 ";
$query4 = "select sum(served) from bigsum where date<=\"".$datestamp1."\" and date >=\"".$datestamp4."\" and site=6 and area=14 ";
$query5 = "select sum(served) from bigsum where date<=\"".$datestamp1."\" and date >=\"".$datestamp7."\" and site=7 and area=15 ";
$query6 = "select sum(served) from bigsum where date<=\"".$datestamp1."\" and date >=\"".$datestamp7."\" and site=8 and area=16 ";
$query7 = "select sum(served) from bigsum where date<=\"".$datestamp1."\" and date >=\"".$datestamp14."\" and site=9 and area=17 ";

// Visitor 1
$v1_result=mysql_query($query1);
$v1_impressions=mysql_result($v1_result,0); 
if(!$v1_impressions) { $v1_impressions = 0; }
$v1_trait = trait_level($v1_impressions,1);
// Visitor 2
$v2_result=mysql_query($query2);
$v2_impressions=mysql_result($v2_result,0); 
if(!$v2_impressions) { $v2_impressions = 0; }
$v2_trait = trait_level($v2_impressions,2);
// Visitor 3
$v3_result=mysql_query($query3);
$v3_impressions=mysql_result($v3_result,0); 
if(!$v3_impressions) { $v3_impressions = 0; }
$v3_trait = trait_level($v3_impressions,3);
// Visitor 4
$v4_result=mysql_query($query4);
$v4_impressions=mysql_result($v4_result,0); 
if(!$v4_impressions) { $v4_impressions = 0; }
$v4_trait = trait_level($v4_impressions,1);
// Visitor 5
$v5_result=mysql_query($query5);
$v5_impressions=mysql_result($v5_result,0); 
if(!$v5_impressions) { $v5_impressions = 0; }
$v5_trait = trait_level($v5_impressions,5);
// Visitor 6
$v6_result=mysql_query($query6);
$v6_impressions=mysql_result($v6_result,0); 
if(!$v6_impressions) { $v6_impressions = 0; }
$v6_trait = trait_level($v6_impressions,6);
// Visitor 7
$v7_result=mysql_query($query7);
$v7_impressions=mysql_result($v7_result,0); 
if(!$v7_impressions) { $v7_impressions = 0; }
$v7_trait = trait_level($v7_impressions,7);

echo "\n  Visitor: Actual Impressions | Expected Impressions \n";
echo "     Visitor 1 Impressions: ".$v1_impressions." - ".$v1_trait." | ".$expected[$day_of_the_week][1]."\n";
echo "     Visitor 2 Impressions: ".$v2_impressions." - ".$v2_trait." | ".$expected[$day_of_the_week][2]."\n";
echo "     Visitor 3 Impressions: ".$v3_impressions." - ".$v3_trait." | ".$expected[$day_of_the_week][3]."\n";
echo "     Visitor 4 Impressions: ".$v4_impressions." - ".$v4_trait." | ".$expected[$day_of_the_week][4]."\n";
echo "     Visitor 5 Impressions: ".$v5_impressions." - ".$v5_trait." | ".$expected[$day_of_the_week][5]."\n";
echo "     Visitor 6 Impressions: ".$v6_impressions." - ".$v6_trait." | ".$expected[$day_of_the_week][6]."\n";
echo "     Visitor 7 Impressions: ".$v7_impressions." - ".$v7_trait." | ".$expected[$day_of_the_week][7]."\n";

function trait_level($v_impressions,$visitor){

if(($visitor == 1)||($visitor == 2)){
	if($v_impressions < 2) {
		$trait = "none"; }
	if(($v_impressions >= 2)&&($v_impressions < 5)) {
		$trait = "low"; }
	if(($v_impressions >= 5)&&($v_impressions < 8)) {
		$trait = "medium"; }
	if($v_impressions >= 8) {
		$trait = "high"; }
}
else if(($visitor == 3)||($visitor == 4)){
	if($v_impressions < 2) {
		$trait = "none"; }
	if(($v_impressions >= 2)&&($v_impressions < 5)) {
		$trait = "low"; }
	if(($v_impressions >= 5)&&($v_impressions < 8)) {
		$trait = "medium"; }
	if($v_impressions >= 8) {
		$trait = "high"; }
}
else if(($visitor == 5)||($visitor == 6)){
	if($v_impressions < 6) {
		$trait = "none"; }
	if(($v_impressions >= 6)&&($v_impressions < 12)) {
		$trait = "low"; }
	if(($v_impressions >= 12)&&($v_impressions < 20)) {
		$trait = "medium"; }
	if($v_impressions >= 20) {
		$trait = "high"; }
}
else if($visitor == 7){
	if($v_impressions < 6) {
		$trait = "none"; }
	if(($v_impressions >= 6)&&($v_impressions < 12)) {
		$trait = "low"; }
	if(($v_impressions >= 12)&&($v_impressions < 20)) {
		$trait = "medium"; }
	if($v_impressions >= 20) {
		$trait = "high"; }
}

return $trait;
}
?>


